#导包
import unittest
import requests
from api.login import LoginApi

#定义测试类
class TestLoginApi(unittest.TestCase):
    #前置处理
    def setUp(self):
        self.login_api = LoginApi()
        self.session = requests.Session()
    #后置处理
    def tearDown(self):
        self.session.close()

    #定义测试方法
    #登录成功
    def test01_login_success(self):
        #调用验证码接口并且断言
        response = self.login_api.get_verity_code(self.session)
        self.assertEqual(200,response.status_code)
        self.assertIn("image",response.headers.get("Content-Type"))
        #调用登录接口并且断言
        response = self.login_api.login(self.session,"13620600019","123456","8888")
        print(response.json())
        self.assertEqual(200,response.status_code)
        self.assertEqual(1,response.json().get("status"))
        self.assertIn("登陆成功",response.json().get("msg"))

    #账号不存在
    def test02_login_fail(self):
        response = self.login_api.get_verity_code(self.session)
        self.assertEqual(200,response.status_code)
        self.assertIn("image",response.headers.get("Content-Type"))
        response = self.login_api.login(self.session,"13620610123","123456","8888")
        print(response.json())
        self.assertEqual(200,response.status_code)
        self.assertEqual(-1,response.json().get("status"))
        self.assertIn("账号不存在", response.json().get("msg"))

    #密码错误
    def test03_login_password(self):
        response = self.login_api.get_verity_code(self.session)
        self.assertEqual(200,response.status_code)
        self.assertIn("image",response.headers.get("Content-Type"))
        response = self.login_api.login(self.session,"13620600019","123abc","8888")
        print(response.json())
        self.assertEqual(200,response.status_code)
        self.assertEqual(-2,response.json().get("status"))
        self.assertIn("密码错误",response.json().get("msg"))
